package com.owc.controller.admin;

import com.owc.common.context.SessionHelper;
import com.owc.common.enums.FlowTypeEnum;
import com.owc.common.Const.Const;
import com.owc.common.tool.DateUtils;
import com.owc.common.tool.ExcelUtils;
import com.owc.domain.BaseResult;
import com.owc.domain.ExcelData;
import com.owc.domain.dto.admin.AccountFlowDTO;
import com.owc.domain.vo.admin.AccountFlowVo;
import com.owc.service.admin.AccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author Jackies
 * @Date 2019/12/26 23:24
 * @Description TODO://
 **/
@RestController
@RequestMapping("/admin/flow")
public class AccountFlowController {

	@Autowired
	private AccountFlowService accountFlowService;

	@PostMapping(value = "/list")
	public BaseResult<List<AccountFlowDTO>> list(@RequestBody AccountFlowVo accountFlowVo){
		return accountFlowService.list(accountFlowVo);
	}

	@PostMapping(value = "/export")
	public void exportFlow(@RequestBody AccountFlowVo accountFlowVo) throws Exception {
		accountFlowVo.setPageSize(50000);
		BaseResult<List<AccountFlowDTO>> datas = accountFlowService.list(accountFlowVo);
		if(datas.getData() == null){
			return;
		}

		ExcelData data = new ExcelData<>();
		data.setName("sheet1");
		data.setTitles(new ArrayList<>(Arrays.asList(Const._USER_EXPORT_DEFAULT_TABLE)));

		List<AccountFlowDTO> flowDTOS = datas.getData();

		List<List<Object>> rows = new ArrayList<>();

		for (AccountFlowDTO flowDTO : flowDTOS) {
			List<Object> row = new ArrayList<>();
			row.add(flowDTO.getFlowId());
			row.add(flowDTO.getTokenName());
			Optional<FlowTypeEnum> optional = FlowTypeEnum.match(flowDTO.getType());
			if(!optional.isPresent()){
				row.add("未知");
			}else{
				row.add(optional.get().getDesc());
			}
			row.add(flowDTO.getTransferAmount());
			row.add(flowDTO.getCreateTime());
			row.add(flowDTO.getBalance() == null ? 0.0 : flowDTO.getBalance().toPlainString());
			row.add(flowDTO.getWalletBalance() == null ? 0.0 : flowDTO.getWalletBalance().toPlainString());
			row.add(flowDTO.getPhone());
			row.add(flowDTO.getWalletAddress());
			row.add(flowDTO.getTxHash());
			rows.add(row);
		}

		data.setRows(rows);
		ExcelUtils.exportExcel(SessionHelper.getContextResponse(),Const._EXPORT_DEFAULT_FILE_NAME + "_" + DateUtils.formatYMDHMS(new Date()),data);
	}

}
